<?php
	require("./conn.php");
	//获取文件扩展名
	function getsufix($file_name) {
		$extend = explode(".", $file_name);
		$va = count($extend) - 1;
		return strtolower($extend[$va]);
	}
	//随机生成文件名函数
	function GetID($prefix) {
		//第一步:初始化种子 
		//microtime(); 是个数组
		$seedstr =split(" ",microtime(),5); 
		$seed =$seedstr[0]*10000;   
		//第二步:使用种子初始化随机数发生器 
		srand($seed);   
		//第三步:生成指定范围内的随机数 
		$random =rand(1000,10000);
		$sufix = getsufix($prefix);
		$filename = date("YmdHis", time()).$random.'.'.$sufix;
	  
		return $filename;
	}
	if ((($_FILES["file"]["type"] == "image/gif")
	|| ($_FILES["file"]["type"] == "image/jpeg")
	|| ($_FILES["file"]["type"] == "image/pjpeg"))){
		if ($_FILES["file"]["error"] > 0){
			echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
		}
		else{
			echo "Upload: " . $_FILES["file"]["name"] . "<br />";
			echo "Type: " . $_FILES["file"]["type"] . "<br />";
			echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
			echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
			if (file_exists("upload/" . $_FILES["file"]["name"])){
			echo $_FILES["file"]["name"] . " already exists. ";
			}
			else{
				$_FILES["file"]["name"] = GetID($_FILES["file"]["name"]) ;//改变文件名
				move_uploaded_file($_FILES["file"]["tmp_name"],//移动临时文件
				"upload/" . $_FILES["file"]["name"]);//保存文件的名
				echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
				date_default_timezone_set(PRC);//解决时间函数时差问题
				$time = date("Y-m-d");
				$name = $_FILES["file"]["name"];
				$sql = "insert into app_image(url,create_time)values('$name','$time')";
				$result = mysql_query($sql);
			}
		}
	}
	else{
		echo "Invalid file";
		//echo $_FILES["file"]["type"];
	}
?>